package com.firexxx.study.lock.task;

import com.firexxx.study.performancetest.task.IFixedTimeConsumeTask;
import com.firexxx.study.performancetest.timeconsume.ITimeConsume;
import lombok.Data;

import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

/**
 * TODO
 *
 * @author fireXXX
 * @date 2020/11/19 0:14
 **/
public class LockConsumeTask extends IFixedTimeConsumeTask {

    private ReadWriteLock lock = new ReentrantReadWriteLock();
    private long count = 0;

    @Override
    public Object runOne() {
        lock.readLock().lock();
        try {
            getTimeConsume().run();
            count++;
        } catch (Exception e) {
        } finally {
            lock.readLock().unlock();
        }

        return null;
    }

    @Override
    public void setCount(long count) {
        this.count = count;
    }

    @Override
    public long getCount() {
        return count;
    }

}